$(function () {
    var myNetWorkDisk_tabs = $("#myNetWorkDisk_tabs");
    var fileImport = $("#fileImport");
    var importFileForm = $("#importFileForm");
    var netDisk_treegrid = $("#tt");
    var import_template = $('#import_template');

    netDisk_treegrid.treegrid({
        url: 'allParentDisk',
        idField: 'id',
        treeField: 'name',
        fit: true,
        fitColumns: true,
        animate: true,
        columns: [[
            {title: '文件名', field: 'name', width: 180},
            {title: '创建时间', field: 'createTime', width: 60,align:'center'}
        ]],
        toolbar: "#myNetWorkDisk_toolbar",
        onDblClickRow:function (row) {
            netDisk_treegrid.treegrid("toggle",row.id);
        }
    });

    fileImport.filebox({
        buttonText: '选择文件',
        buttonAlign: 'right'
    });
    myNetWorkDisk_tabs.tabs({
        border: false
    })

    var cmdObj = {
        //跳出上传窗口
        addMyNetWorkDisk: function () {
            var selectedRow = netDisk_treegrid.treegrid("getSelected");
            if(!selectedRow){
                $.messager.alert("提示","请选择一个目录进行文件上传","error");
            }
            if(selectedRow.iconCls == "icon-fileholder"){
            import_template.dialog('open');
            document.getElementById("importFileForm").style.display = "block";
            }else{
                $.messager.alert("提示","文件类型不能再上传文件!","error");
            }
        },
        //下载
        downloadClassroom: function () {
            var selectedRow = netDisk_treegrid.treegrid("getSelected");
            if (!selectedRow) {
                $.messager.alert("提示", "请选中一个文件或文件夹", "error");
                return;
            }

            var url = selectedRow.url + "&filename=" + selectedRow.text;
            console.log(url);

        },
        //删除
        deleteMyNetWorkDisk: function () {
            var selectedRow = netDisk_treegrid.treegrid("getSelected");
            if (!selectedRow) {
                $.messager.alert("提示", "请选中一个文件或文件夹", "error");
                return;
            }
            //弹出确认框
            if (selectedRow.children) {
                $.messager.confirm("提示", "您确定要删除该目录已经该目录下所有的文件吗", function (yes) {
                    if (yes) {
                        $.post("/netDisk/deleteData", {id: selectedRow.id}, function (data) {
                            if (data.success) {
                                $.messager.alert("提示", "删除成功", "info", function () {
                                    cmdObj.reloadClassroom();
                                });
                            } else {
                                $.messager.alert("提示", data.msg, "error");
                            }
                        })
                    }
                });
            } else {
                $.messager.confirm("提示", "您确定要删除该文件吗", function (yes) {
                    if (yes) {
                        $.post("/netDisk/deleteData", {id: selectedRow.id}, function (data) {
                            if (data.success) {
                                $.messager.alert("提示", "删除成功", "info", function () {
                                    cmdObj.reloadClassroom();
                                });
                            } else {
                                $.messager.alert("提示", data.msg, "error");
                            }
                        })
                    }
                });
            }
        },
        //新建文件夹
        buildMyNetWorkDisk: function () {
            var selectedRow = netDisk_treegrid.treegrid("getSelected");
            if(!selectedRow){
                $.messager.prompt("提示", "请填写要新增的文件夹名称:", function (name) {
                    if (name) {
                        $.post("/netDisk/saveDataFolder", {fileName: name}, function (data) {
                            if (data.success) {
                                $.messager.alert("提示", "新建成功", "info",function () {
                                    cmdObj.reloadClassroom();
                                });
                            } else {
                                $.messager.alert("提示", data.msg, "error");
                            }
                        })
                    }
                });
            }else if(selectedRow.iconCls == "icon-fileholder"){
                $.messager.prompt("提示", "请填写要新增的文件夹名称:", function (name) {
                    if (name) {
                        $.post("/netDisk/saveDataFolder", {fileName: name,id:selectedRow.id}, function (data) {
                            if (data.success) {
                                $.messager.alert("提示", "新建成功", "info",function () {
                                    cmdObj.reloadClassroom();
                                });
                            } else {
                                $.messager.alert("提示", data.msg, "error");
                            }
                        })
                    }
                });
            }else{
                $.messager.alert("提示","文件类型中无法创建文件夹","error");
            }
        },
        //刷新
        reloadClassroom: function () {
            netDisk_treegrid.treegrid("reload");
        },
        //关闭
        closeImportClick: function () {
            importFileForm.form("clear");
            import_template.dialog('close');
        }, //上传
        importFileClick: function () {
            var selectedRow = netDisk_treegrid.treegrid("getSelected");
            if (selectedRow) {
                var parentId = selectedRow.id;
                var name = selectedRow.name;
            }
            var file = fileImport.filebox("files");
            //获取文件名称
            var fileName = file[0].name;
            //使用ajax方式提交
            importFileForm.form("submit", {
                url: '/netDisk/postData',
                onSubmit: function (param) {
                    param['parentId'] = parentId;
                    param['fileName'] = fileName;
                    param['name'] = name
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("提示", "上传成功", "info", function () {
                            cmdObj.closeImportClick();
                            cmdObj.reloadClassroom();
                        });
                    } else {
                        $.messager.alert("提示", data.msg, "error");
                    }
                }
            })
        }
    }


    //按钮的点击事件通过js来绑定
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });
});
